<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\Carts;
use App\Repository\Models\Regions;
use App\Repository\Models\UserInvoice;
use App\Repository\Repositories\Interfaces\AddressRepository;
use App\Repository\Repositories\Interfaces\CartsRepository;
use App\Repository\Repositories\Interfaces\InvoicesRepository;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\UserInvoiceRepository;
use Illuminate\Http\Request;

/**
 * 地址控制器
 */
class AddressController extends BaseController
{
    /**
     * @var AddressRepository
     */
    protected $address_repo;

    protected $user_id;

    public function __construct(AddressRepository $address_repo)
    {
        parent::__construct();
        $this->address_repo = $address_repo;
        $this->user_id = array_get($this->user, 'id');
        //用户只能操作自身所属数据
        $this->address_repo->scopeQuery(function ($query) {
            return $query->where('user_id', $this->user_id);
        });
    }

    public function index(Request $request)
    {
        $this->validate($request, [
            'type' => 'in:page,all'
        ]);
        $type = $request->get('type', 'page');
        if($type == 'page'){
            $address = $this->address_repo->orderBy('is_default', 'desc')->paginate($this->perPage);
            return $this->success(ErrorConst::SUCCESS_CODE, $address, true);
        }else{
            $address = $this->address_repo->orderBy('is_default', 'desc')->all();
            return $this->success(ErrorConst::SUCCESS_CODE, $address['data']);
        }
    }

    public function show($id)
    {
        $address = $this->address_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $address['data']);
    }

    /**
     * 创建
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'area_code' => 'required|integer',
            'address' => 'required',
            'receiver' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
        ]);
        $id = $request->get('id', null);
        $data = $request->all(['province_code', 'city_code', 'area_code', 'address', 'receiver', 'phone']);
        $merger_name=Regions::query()->where('id',$data['area_code'])->get(['merger_name'])->toArray();
        $merger_name_arr=explode(',',$merger_name[0]['merger_name']);

        $data['address_head']='';
        foreach($merger_name_arr as $k=>$v){
            if($k!=0){
                $data['address_head'].=$v;
            }
        }
        $data['user_id'] = $this->user_id;
        $data['address'] = $data['address_head'].$data['address'];

        $ret = $this->address_repo->updateOrCreate(['id' => $id], $data);
        $isDef=$request->all(['is_defult']);
        if(isset($isDef['is_defult']) && !empty($isDef['is_defult'])){
            if($ret){
                $this->address_repo->setDefault($ret['data']['id']);
            }
        }
//        return $this->success(ErrorConst::SUCCESS_CODE, $ret);

        return $this->noDataReturn($ret ? true : false);
    }

    /**
     * 验证是否已经存在默认地址
     */
    public function isDefult(Request $request){
        $data = $request->all(['is_defult']);
        if(isset($data['is_defult']) && !empty($data['is_defult'])){
            $isDef=$this->address_repo->scopeQuery(function($sql){
                return $sql->where('is_defult',1)->where('user_id', $this->user_id);
            })->all();
            if($isDef){
                return $this->noDataReturn(false);
            }else{
                return $this->noDataReturn(true);
            }
        }
        return $this->noDataReturn(true);

    }

    /**
     * 更新
     * @param Request $request
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'province_code' => 'required|integer',
            'city_code' => 'required|integer',
            'area_code' => 'required|integer',
            'address' => 'required',
            'receiver' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
        ]);
        $data = $request->all(['province_code', 'city_code', 'area_code', 'address', 'receiver', 'phone']);
        $merger_name=Regions::query()->where('id',$data['area_code'])->get(['merger_name'])->toArray();
        $merger_name_arr=explode(',',$merger_name[0]['merger_name']);

        $data['address_head']='';
        foreach($merger_name_arr as $k=>$v){
            if($k!=0){
                $data['address_head'].=$v;
            }
        }
        $data['address'] = $data['address_head'].$data['address'];

        $ret = $this->address_repo->update($data, $id);

        return $this->noDataReturn($ret ? true : false);
    }

    public function updateSetDefault($id)
    {
        $ret = $this->address_repo->setDefault($id);
        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        $ret = $this->address_repo->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }
}
